package data
{
	import com.maclema.mysql.MySqlToken;
	import com.maclema.mysql.ResultSet;
	
	import controls.DataHub;
	import controls.events.ServiceEvent;
	
	import flash.events.EventDispatcher;
	
	public class ClassSection extends EventDispatcher
	{
		public var CRN:int;
		public var professor:Professor;
		public var section:String;
		public var location:String;
		public var id:int;
		public var times:Array;
		public var type:String;
		public var semester:String;
		
		private var hub:DataHub = DataHub.instance;
		
		public var pendingRequest:Boolean;
		
		public function ClassSection(sectionInfo:Object = null)
		{
			times = [];
			pendingRequest = false;
			
			if(sectionInfo != null)
			{
				id = sectionInfo.id;
				location = sectionInfo.Location;
				
				professor = new Professor(sectionInfo.Instructor);
				professor.addEventListener(ServiceEvent.PROFESSOR_LOADED, onProfessorReady);
				professor = hub.getProfessor(professor, onProfessorReady)
			
				CRN = sectionInfo.CRN;
				type = sectionInfo.Type;
				section = sectionInfo.Section;
				semester = sectionInfo.Semester;
				
				makeTimes(sectionInfo);
				
				getGPAInfo();
			}
		}
		
		// --------------- Data Fetching and Parsing ----------------- //
		private function onProfessorReady(evt:ServiceEvent):void
		{
			professor = evt.data as Professor;
			hub.addProfessor(professor);
			
			if(pendingRequest)
			{
				professor.addEventListener(ServiceEvent.COURSES_LOADED, onProfCoursesLoaded);
				professor.getGPAInfo();				
			}
			
			this.dispatchEvent(new ServiceEvent(ServiceEvent.COURSES_LOADED, this));
		}
		
		private function onProfCoursesLoaded(evt:ServiceEvent):void
		{
			professor = evt.data as Professor;
			hub.addProfessor(professor);
			this.dispatchEvent(new ServiceEvent(ServiceEvent.COURSES_LOADED, this));
		}
		
		private function onProfessorLoaded(evt:ServiceEvent):void
		{
			professor = evt.data as Professor;
			hub.addProfessor(professor);
			this.dispatchEvent(new ServiceEvent(ServiceEvent.COURSES_LOADED, this));
		}
		
		public function getGPAInfo():void
		{
			//var requestor:DataRequestor = DataRequestor.instance;
			//requestor.getSectionGPA(this, parseGPA);
		}
		
		private function parseGPA(sqlData:Object, token:MySqlToken):void
		{
			var rs:ResultSet = sqlData as ResultSet;
			for each(var row:Object in rs.getRows())
			{
				
			}
		}
		
		private function makeTimes(sectionInfo:Object):void
		{
			var days:String = sectionInfo.Days;
			if(days == null)	return;
			
			var times:Array = String(sectionInfo.Time).split(" - ");
			days = checkDay(days, "M", MeetingTime.MONDAY, times);
			days = checkDay(days, "T", MeetingTime.TUESDAY, times);
			days = checkDay(days, "W", MeetingTime.WEDNESDAY, times);
			days = checkDay(days, "Th", MeetingTime.THURSDAY, times);
			days = checkDay(days, "F", MeetingTime.FRIDAY, times);
		}
		
		private function checkDay(days:String, dayString:String, value:String, times:Array):String
		{
			var dayIndex:int = days.indexOf(dayString);
			if(dayIndex != -1)
				times.push(new MeetingTime({day: value, 
											startTime: times[0],
											endTime: times[1]}));
			days = days.slice(dayIndex + dayString.length);
			return days;
		}
		// --------------- END Data Fetching and Parsing ----------------- //
	}
}